package main.java.indi.zyj.acm;

public class Trap {

    public int trap(int[] height) {

        int left = 0, right = height.length - 1, maxLeft = 0, maxRight = 0, res = 0;
        while (left < right) {
            maxLeft = Math.max(height[left], maxLeft);
            maxRight = Math.max(height[right], maxRight);
            res += maxLeft > maxRight ? maxRight - height[right] : maxLeft - height[left];
        }

        return res;

    }

}
